package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import org.apache.xmlbeans.impl.jam.mutable.MElement;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/travelItem")
public class TravelItemController {
    @Reference
    TravelItemService travelItemService;

    //添加
    @PreAuthorize("hasAuthority('TRAVELITEM_ADD')")
    @RequestMapping("/add.do")
    public Result add(@RequestBody TravelItem travelItem){  //@RequestBody 将前台传输的json对象转化为java对象
        try {
            travelItemService.add(travelItem);
            return new Result(true, MessageConstant.ADD_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELITEM_FAIL);
        }
    }

    //分页
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @RequestMapping("/findPage.do")
    public PageResult findPage (@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult = travelItemService.findPage(queryPageBean);
       return pageResult;
     }

    //删除
    @PreAuthorize("hasAuthority('DELETE')")
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            travelItemService.delete(id);
            return  new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    //根据id查询自由行的信息
    @PreAuthorize("hasAuthority('TRAVELITEM_QUERY')")
    @RequestMapping("/getItemById.do")
    public Result getItemById(Integer id){
       TravelItem travelItem =  travelItemService.getItemById(id);
        return new Result(true,MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItem);
    }

    //编辑,修改自由行
    @PreAuthorize("hasAuthority('TRAVELITEM_EDIT')")
    @RequestMapping("/edit.do")
    public Result edit(@RequestBody  TravelItem travelItem){
        try {
            travelItemService.edit(travelItem);
            return new Result(true,MessageConstant.EDIT_TRAVELITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELITEM_FAIL);
        }
    }
    //查询所有的自由行
    @RequestMapping("/findAll.do")
    public Result  findAll(){
     List<TravelItem> travelItemList = travelItemService.findAll();
     return  new Result(true, MessageConstant.QUERY_TRAVELITEM_SUCCESS,travelItemList);
    }
}
